/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Alias;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.MakeObjectOpt;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.SortOpt;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.HardRules.HardRule;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.session.AdminSession;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Arrays;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SearchDirectory
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public static final String BY_NAME = "name";
/*     */   public static final String BY_ID = "id";
/*     */   private static final String SEARCH_DIRECTORY_ACCOUNT_DATA = "SearchDirectoryAccount";
/*  69 */   public static final int MAX_SEARCH_RESULTS = LC.zimbra_directory_max_search_result.intValue();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  76 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  81 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  82 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  84 */     String query = request.getAttribute("query", null);
/*     */     
/*  86 */     int maxResults = (int)request.getAttributeLong("maxResults", MAX_SEARCH_RESULTS);
/*  87 */     int limit = (int)request.getAttributeLong("limit", 2147483647L);
/*  88 */     if (limit == 0) {
/*  89 */       limit = Integer.MAX_VALUE;
/*     */     }
/*  91 */     int offset = (int)request.getAttributeLong("offset", 0L);
/*  92 */     String domain = request.getAttribute("domain", null);
/*  93 */     boolean applyCos = request.getAttributeBool("applyCos", true);
/*  94 */     boolean applyConfig = request.getAttributeBool("applyConfig", true);
/*  95 */     String origAttrsStr = request.getAttribute("attrs", null);
/*  96 */     String sortBy = request.getAttribute("sortBy", null);
/*  97 */     String types = request.getAttribute("types", "accounts");
/*  98 */     boolean sortAscending = request.getAttributeBool("sortAscending", true);
/*  99 */     boolean isCountOnly = request.getAttributeBool("countOnly", false);
/*     */     
/* 101 */     Set<SearchDirectoryOptions.ObjectType> objTypes = SearchDirectoryOptions.ObjectType.fromCSVString(types);
/*     */     
/*     */ 
/* 104 */     if ((objTypes.contains(SearchDirectoryOptions.ObjectType.coses)) && (domain != null))
/*     */     {
/* 106 */       throw ServiceException.INVALID_REQUEST("cannot specify domain with coses flag", null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 111 */     String attrsStr = origAttrsStr;
/* 112 */     if ((objTypes.contains(SearchDirectoryOptions.ObjectType.accounts)) && (attrsStr != null) && (!attrsStr.contains("zimbraMailTransport")))
/*     */     {
/* 114 */       attrsStr = attrsStr + "," + "zimbraMailTransport";
/*     */     }
/* 116 */     if (((objTypes.contains(SearchDirectoryOptions.ObjectType.distributionlists)) || (objTypes.contains(SearchDirectoryOptions.ObjectType.dynamicgroups))) && (attrsStr != null) && (!attrsStr.contains("memberURL")))
/*     */     {
/*     */ 
/* 119 */       attrsStr = attrsStr + "," + "memberURL";
/*     */     }
/*     */     
/* 122 */     String[] attrs = attrsStr == null ? null : attrsStr.split(",");
/* 123 */     Set<String> reqAttrs = attrs == null ? null : new HashSet(Arrays.asList(attrs));
/*     */     
/* 125 */     Element response = zsc.createElement(AdminConstants.SEARCH_DIRECTORY_RESPONSE);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 130 */     if (isDomainAdminOnly(zsc)) {
/* 131 */       if (objTypes.contains(SearchDirectoryOptions.ObjectType.domains)) {
/* 132 */         if ((query != null) && (query.length() > 0)) {
/* 133 */           throw ServiceException.PERM_DENIED("cannot search for domains");
/*     */         }
/* 135 */         domain = getAuthTokenAccountDomain(zsc).getName();
/* 136 */         Domain d = null;
/* 137 */         if (domain != null) {
/* 138 */           d = prov.get(Key.DomainBy.name, domain);
/* 139 */           if (d == null)
/* 140 */             throw AccountServiceException.NO_SUCH_DOMAIN(domain);
/*     */         }
/* 142 */         GetDomain.encodeDomain(response, d, applyConfig, reqAttrs, null);
/* 143 */         response.addAttribute("more", false);
/* 144 */         response.addAttribute("searchTotal", 1L);
/* 145 */         return response;
/*     */       }
/*     */       
/*     */ 
/* 149 */       if (objTypes.contains(SearchDirectoryOptions.ObjectType.coses)) {
/* 150 */         throw ServiceException.PERM_DENIED("cannot search for coses");
/*     */       }
/* 152 */       if (domain == null) {
/* 153 */         domain = getAuthTokenAccountDomain(zsc).getName();
/*     */       } else {
/* 155 */         checkDomainRight(zsc, domain, AdminRight.PR_ALWAYS_ALLOW);
/*     */       }
/*     */     }
/*     */     
/* 159 */     Domain d = null;
/* 160 */     if (domain != null) {
/* 161 */       d = prov.get(Key.DomainBy.name, domain);
/* 162 */       if (d == null) {
/* 163 */         throw AccountServiceException.NO_SUCH_DOMAIN(domain);
/*     */       }
/*     */     }
/* 166 */     AdminAccessControl aac = AdminAccessControl.getAdminAccessControl(zsc);
/* 167 */     AdminAccessControl.SearchDirectoryRightChecker rightChecker = new AdminAccessControl.SearchDirectoryRightChecker(aac, prov, reqAttrs);
/*     */     
/*     */ 
/*     */ 
/* 171 */     AdminSession session = (AdminSession)getSession(zsc, Session.Type.ADMIN);
/*     */     
/* 173 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 174 */     options.setDomain(d);
/* 175 */     options.setTypes(types);
/* 176 */     options.setMaxResults(maxResults);
/* 177 */     options.setFilterString(ZLdapFilterFactory.FilterId.ADMIN_SEARCH, query);
/* 178 */     options.setReturnAttrs(attrs);
/* 179 */     options.setSortOpt(sortAscending ? SearchDirectoryOptions.SortOpt.SORT_ASCENDING : SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/* 180 */     options.setSortAttr(sortBy);
/* 181 */     options.setConvertIDNToAscii(true);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 186 */     options.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*     */     
/* 188 */     int limitMax = offset + limit;
/* 189 */     List<NamedEntry> accounts; List<NamedEntry> accounts; if (session != null) {
/* 190 */       accounts = session.searchDirectory(options, offset, rightChecker);
/*     */     } else {
/* 192 */       accounts = prov.searchDirectory(options);
/* 193 */       accounts = rightChecker.getAllowed(accounts, limitMax);
/*     */     }
/*     */     
/* 196 */     if (isCountOnly) {
/* 197 */       response.addAttribute("num", accounts.size());
/*     */     }
/*     */     else {
/* 200 */       String[] origAttrs = origAttrsStr == null ? null : origAttrsStr.split(",");
/* 201 */       Set<String> origReqAttrs = origAttrs == null ? null : Sets.newHashSet(Arrays.asList(origAttrs));
/*     */       
/* 203 */       long start = System.currentTimeMillis();
/*     */       
/* 205 */       for (int numEntries = offset; (numEntries < limitMax) && (numEntries < accounts.size()); numEntries++) {
/* 206 */         NamedEntry entry = (NamedEntry)accounts.get(numEntries);
/*     */         
/* 208 */         boolean applyDefault = true;
/*     */         
/* 210 */         if ((entry instanceof Account)) {
/* 211 */           applyDefault = applyCos;
/* 212 */           setAccountDefaults((Account)entry);
/* 213 */         } else if ((entry instanceof Domain)) {
/* 214 */           applyDefault = applyConfig;
/*     */         }
/*     */         
/* 217 */         encodeEntry(prov, response, entry, applyDefault, origReqAttrs, aac);
/*     */       }
/* 219 */       if (ZimbraLog.search.isTraceEnabled()) {
/* 220 */         ZimbraLog.search.trace("SearchDirectory - encoding entries %s i=%s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()), Integer.valueOf(numEntries) });
/*     */       }
/*     */       
/*     */ 
/* 224 */       response.addAttribute("more", numEntries < accounts.size());
/* 225 */       response.addAttribute("searchTotal", accounts.size());
/*     */     }
/* 227 */     return response;
/*     */   }
/*     */   
/*     */   private void setAccountDefaults(Account entry) throws ServiceException
/*     */   {
/* 232 */     Boolean isDefaultSet = (Boolean)entry.getCachedData("SearchDirectoryAccount");
/* 233 */     if ((isDefaultSet == null) || (isDefaultSet == Boolean.FALSE)) {
/* 234 */       entry.setAccountDefaults(true);
/* 235 */       entry.setCachedData("SearchDirectoryAccount", Boolean.TRUE);
/*     */     }
/*     */   }
/*     */   
/*     */   static void encodeEntry(Provisioning prov, Element parent, NamedEntry entry, boolean applyDefault, Set<String> reqAttrs, AdminAccessControl aac)
/*     */     throws ServiceException
/*     */   {
/* 242 */     if ((entry instanceof CalendarResource)) {
/* 243 */       ToXML.encodeCalendarResource(parent, (CalendarResource)entry, applyDefault, reqAttrs, aac.getAttrRightChecker(entry, EnumSet.of(HardRules.HardRule.DELEGATED_ADMIN_CANNOT_ACCESS_GLOBAL_ADMIN)));
/*     */ 
/*     */     }
/* 246 */     else if ((entry instanceof Account)) {
/* 247 */       ToXML.encodeAccount(parent, (Account)entry, applyDefault, true, reqAttrs, aac.getAttrRightChecker(entry, EnumSet.of(HardRules.HardRule.DELEGATED_ADMIN_CANNOT_ACCESS_GLOBAL_ADMIN)));
/*     */ 
/*     */     }
/* 250 */     else if ((entry instanceof DistributionList)) {
/* 251 */       GetDistributionList.encodeDistributionList(parent, (DistributionList)entry, false, false, reqAttrs, aac.getAttrRightChecker(entry));
/*     */     }
/* 253 */     else if ((entry instanceof DynamicGroup))
/*     */     {
/*     */ 
/* 256 */       GetDistributionList.encodeDistributionList(parent, (DynamicGroup)entry, false, false, reqAttrs, null);
/*     */     }
/* 258 */     else if ((entry instanceof Alias)) {
/* 259 */       encodeAlias(parent, prov, (Alias)entry, reqAttrs);
/* 260 */     } else if ((entry instanceof Domain)) {
/* 261 */       GetDomain.encodeDomain(parent, (Domain)entry, applyDefault, reqAttrs, aac.getAttrRightChecker(entry));
/*     */     }
/* 263 */     else if ((entry instanceof Cos)) {
/* 264 */       GetCos.encodeCos(parent, (Cos)entry, reqAttrs, aac.getAttrRightChecker(entry));
/*     */     }
/*     */   }
/*     */   
/*     */   private static void encodeAlias(Element e, Provisioning prov, Alias a, Set<String> reqAttrs) throws ServiceException
/*     */   {
/* 270 */     Element ealias = e.addElement("alias");
/* 271 */     ealias.addAttribute("name", a.getUnicodeName());
/* 272 */     ealias.addAttribute("id", a.getId());
/* 273 */     ealias.addAttribute("targetName", a.getTargetUnicodeName(prov));
/*     */     
/* 275 */     TargetType tt = a.getTargetType(prov);
/* 276 */     if (tt != null) {
/* 277 */       ealias.addAttribute("type", tt.getCode());
/*     */     }
/*     */     
/* 280 */     Map attrs = a.getUnicodeAttrs();
/*     */     
/* 282 */     ToXML.encodeAttrs(ealias, attrs, reqAttrs, null);
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 288 */     relatedRights.add(Rights.Admin.R_getAccount);
/* 289 */     relatedRights.add(Rights.Admin.R_getCalendarResource);
/* 290 */     relatedRights.add(Rights.Admin.R_getDistributionList);
/* 291 */     relatedRights.add(Rights.Admin.R_getDomain);
/* 292 */     relatedRights.add(Rights.Admin.R_getCos);
/* 293 */     relatedRights.add(Rights.Admin.R_listAccount);
/* 294 */     relatedRights.add(Rights.Admin.R_listCalendarResource);
/* 295 */     relatedRights.add(Rights.Admin.R_listDistributionList);
/* 296 */     relatedRights.add(Rights.Admin.R_listDomain);
/* 297 */     relatedRights.add(Rights.Admin.R_listCos);
/*     */     
/* 299 */     notes.add("If the authenticated admin does not have the corresponding list{Entry} right for an entry, the entry is skipped in the getAllXXX/searchXXX/searchDirectoryResponse,  no PERM_DENIED exception will be thrown. Attributes that are not allowed to be get by the authenticated admin will be returned as <a n=\"{attr-name}\" pd=\"1\"/>.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/SearchDirectory.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */